package com.hd.trans.utils;

import android.text.TextUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Author: yangtianwen
 * Time: 2021/12/8
 * Des:
 */
public class DateFormatUtil {

    /**
     * @param time 毫秒
     * @return
     */
    public static String formatAudioTime(long time) {
        long minutes = time / (1000 * 60);
        long second = (time - minutes * 1000 * 60) / 1000;
        long millis = (time - minutes * 1000 * 60 - second * 1000);
        StringBuilder sb = new StringBuilder();
        if (minutes < 10) {
            sb.append("0");
        }
        sb.append(minutes);
        sb.append(":");
        if (second < 10) {
            sb.append("0");
        }
        sb.append(second);
        sb.append(":");
        sb.append("0");
        sb.append(millis / 100);
        return sb.toString();
    }

    /**
     * 精确到秒
     *
     * @param time 毫秒
     * @return
     */
    public static String formatAudioTimeToSecond(long time) {
        long minutes = time / (1000 * 60);
        long second = (time - minutes * 1000 * 60) / 1000;
        long millis = (time - minutes * 1000 * 60 - second * 1000);
        StringBuilder sb = new StringBuilder();
        if (minutes < 10) {
            sb.append("0");
        }
        sb.append(minutes);
        sb.append(":");
        if (second < 10) {
            sb.append("0");
        }
        sb.append(second);
        return sb.toString();
    }

    /**
     * 将unix时间戳转为日期
     *
     * @return 日期，举例：1970-01-01 HH:mm
     */
    public static String getDate(long millis) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()).format(new Date(millis));
    }

    /**
     * 获取时间（年月日）
     */
    public static String[] getDateByCalendar(long time) {
        Calendar cal = Calendar.getInstance();
        String[] times = new String[3];
        Date date = new Date(time);
        cal.setTime(date);
        times[0] = cal.get(Calendar.YEAR) + "";
        times[1] = (cal.get(Calendar.MONTH) + 1) + "";//calendar月份从0-11
        times[2] = cal.get(Calendar.DAY_OF_MONTH) + "";
        return times;
    }

    /**
     * 根据毫秒转化为年月日时分秒
     *
     * @param second
     * @return
     */
    public static String getTime(long second,String type) {
        if (TextUtils.isEmpty(type)){
            type="yyyy-MM-dd HH:mm";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(type);
        String hms = formatter.format(second);
        return hms;
    }

    /**
     * 根据毫秒转化秒
     *
     * @return
     */
    public static long getSecondTime(long duration) {
        long second = Math.round((float)duration/1000) ;
        return second;
    }
}
